package com.cook.controller;

import com.cook.dto.recipes.StepsDTO;
import com.cook.result.Result;
import com.cook.service.StepsService;
import com.cook.vo.recipes.StepsVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/steps")
@Slf4j
public class StepsController {

    @Autowired
    private StepsService stepsService;

    /**
     * 根据食谱ID查询步骤列表
     */
    @GetMapping("/recipe/{recipeId}")
    public Result<List<StepsVO>> getStepsByRecipeId(@PathVariable Long recipeId) {
        log.info("查询食谱步骤: recipeId={}", recipeId);

        try {
            List<StepsVO> steps = stepsService.getStepsByRecipeId(recipeId);
            return Result.success(steps);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询步骤失败: ", e);
            return Result.error(500, "查询步骤失败");
        }
    }

    /**
     * 根据步骤ID查询步骤详情
     */
    @GetMapping("/{stepId}")
    public Result<StepsVO> getStepById(@PathVariable Long stepId) {
        log.info("查询步骤详情: stepId={}", stepId);

        try {
            StepsVO step = stepsService.getStepById(stepId);
            return Result.success(step);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (RuntimeException e) {
            log.warn("查询失败: {}", e.getMessage());
            return Result.error(404, e.getMessage());
        } catch (Exception e) {
            log.error("查询步骤详情失败: ", e);
            return Result.error(500, "查询步骤失败");
        }
    }

    /**
     * 添加步骤
     */
    @PostMapping
    public Result<Long> addStep(@RequestBody StepsDTO stepsDTO) {
        log.info("添加步骤: recipeId={}", stepsDTO.getRecipeId());

        try {
            Long stepId = stepsService.addStep(stepsDTO);
            return Result.success("步骤添加成功", stepId);
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("添加步骤失败: ", e);
            return Result.error(500, "添加步骤失败");
        }
    }

    /**
     * 批量添加步骤
     */
    @PostMapping("/batch")
    public Result<Boolean> batchAddSteps(@RequestBody List<StepsDTO> stepsDTOList) {
        log.info("批量添加步骤: 共 {} 个", stepsDTOList.size());

        try {
            boolean success = stepsService.batchAddSteps(stepsDTOList);
            if (success) {
                return Result.success("步骤批量添加成功", true);
            } else {
                return Result.error("步骤批量添加失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("批量添加步骤失败: ", e);
            return Result.error(500, "批量添加步骤失败");
        }
    }

    /**
     * 更新步骤
     */
    @PutMapping
    public Result<Boolean> updateStep(@RequestBody StepsDTO stepsDTO) {
        log.info("更新步骤: stepId={}", stepsDTO.getStepId());

        try {
            boolean success = stepsService.updateStep(stepsDTO);
            if (success) {
                return Result.success("步骤更新成功", true);
            } else {
                return Result.error("步骤更新失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("更新步骤失败: ", e);
            return Result.error(500, "更新步骤失败");
        }
    }

    /**
     * 删除步骤
     */
    @DeleteMapping("/{stepId}")
    public Result<Boolean> deleteStep(@PathVariable Long stepId) {
        log.info("删除步骤: stepId={}", stepId);

        try {
            boolean success = stepsService.deleteStep(stepId);
            if (success) {
                return Result.success("步骤删除成功", true);
            } else {
                return Result.error("步骤删除失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除步骤失败: ", e);
            return Result.error(500, "删除步骤失败");
        }
    }

    /**
     * 删除食谱的所有步骤
     */
    @DeleteMapping("/recipe/{recipeId}")
    public Result<Boolean> deleteStepsByRecipeId(@PathVariable Long recipeId) {
        log.info("删除食谱所有步骤: recipeId={}", recipeId);

        try {
            boolean success = stepsService.deleteStepsByRecipeId(recipeId);
            if (success) {
                return Result.success("食谱步骤删除成功", true);
            } else {
                return Result.error("食谱步骤删除失败");
            }
                    } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("删除食谱步骤失败: ", e);
            return Result.error(500, "删除步骤失败");
        }
    }

    /**
     * 重新排序步骤
     */
    @PostMapping("/recipe/{recipeId}/reorder")
    public Result<Boolean> reorderSteps(@PathVariable Long recipeId) {
        log.info("重新排序步骤: recipeId={}", recipeId);

        try {
            boolean success = stepsService.reorderSteps(recipeId);
            if (success) {
                return Result.success("步骤重新排序成功", true);
            } else {
                return Result.error("步骤重新排序失败");
            }
        } catch (IllegalArgumentException e) {
            log.warn("参数错误: {}", e.getMessage());
            return Result.error(400, e.getMessage());
        } catch (Exception e) {
            log.error("重新排序步骤失败: ", e);
            return Result.error(500, "重新排序步骤失败");
        }
    }
}

